import { _decorator, Component, Node, Prefab, resources, instantiate } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('TaskController')
export abstract class TaskController extends Component {
    public node: Node;

    protected id: number;
    protected type: number;
    protected completionConditions: number;
    protected completedQuantity: number;
    protected prefab: Prefab;

    constructor(node: Node, type: number) {
        super();
        this.node = node;
        this.type = type;
    }

    start() {

    }

    update(deltaTime: number) {

    }

    public loadPrefabCell(prefabPath: string, data: object): TaskController {
        this.setProperty(data);
        resources.load(prefabPath, Prefab, (err, prefab) => {
            this.prefab = prefab;
            const item = this.createPrefab(data);
            this.node.addChild(item);
        })

        this.customLoadPrefabCell(prefabPath, data);

        return this;
    }

    protected setProperty(data: Object) {
        this.id = data['id'];
        this.completionConditions = data['CompletionConditions'];
        this.completedQuantity = data['CompletedQuantity'];
    }

    protected createPrefab(data: Object) {
        let item: Node = instantiate(this.prefab);
        return item;
    }

    protected abstract customLoadPrefabCell(prefabPath: string, data: object): void;
    protected abstract customEventListener(prefabPath: string, data: object): void;
}


